package com.jlastu.dlxy_androidapp.view.setting;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.AddressAdapter;
import com.jlastu.dlxy_androidapp.entity.Address;

import java.util.ArrayList;
import java.util.List;


public class AddressSettingsActivity extends AppCompatActivity {

    private List<Address> addressList = new ArrayList<>();
    private AddressAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_settings);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        adapter = new AddressAdapter(this, addressList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        Button addAddressButton = findViewById(R.id.addAddressButton);
        addAddressButton.setOnClickListener(v -> showAddAddressDialog());
    }


    private void showAddAddressDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_add_address, null);
        builder.setView(dialogView);

        EditText editTextName = dialogView.findViewById(R.id.editTextName);
        EditText editTextPhone = dialogView.findViewById(R.id.editTextPhone);
        EditText editTextAddress = dialogView.findViewById(R.id.editTextAddress);

        builder.setTitle("添加地址信息")
                .setPositiveButton("保存", (dialog, id) -> {
                    String name = editTextName.getText().toString().trim();
                    String phone = editTextPhone.getText().toString().trim();
                    String address = editTextAddress.getText().toString().trim();

                    if (!name.isEmpty() && !phone.isEmpty() && !address.isEmpty()) {
                        addressList.add(new Address(name, phone, address));
                        adapter.notifyItemInserted(addressList.size() - 1);
                    }
                })
                .setNegativeButton("取消", (dialog, id) -> dialog.dismiss());

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }



}